package xyz.imeans.w.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import xyz.imeans.w.service.CreateService;

@Slf4j
@Data
@Component
@ConditionalOnProperty(prefix = "create-data", name = "enable", havingValue = "true")
public class CreateDataHandle implements CommandLineRunner {

    private final CreateDataConfig createDataConfig;
    public CreateDataHandle(CreateDataConfig createDataConfig) {
        this.createDataConfig = createDataConfig;
    }

    @Override
    public void run(String... args) throws Exception {
        for (CreateDataConfig.Config config : createDataConfig.getConfig()) {
            log.info("生成{}数据:存储{},数据条数{}", config.getModel(), config.getSource(), config.getCount());

            String source = config.getSource();
            if("mysql".equals(source)) {
                CreateService service = InitCreateFactory.getCreateService(CreateEnum.Mysql);
                service.exec(config);
            } else if("dm".equals(source)) {
                CreateService service = InitCreateFactory.getCreateService(CreateEnum.DM);
                service.exec(config);
            } else if("es".equals(source)) {
                CreateService service = InitCreateFactory.getCreateService(CreateEnum.ES);
                service.exec(config);
            } else if("doris".equals(source)) {
                CreateService service = InitCreateFactory.getCreateService(CreateEnum.DORIS);
                service.exec(config);
            }
        }
    }

}
